package com.momo.common.core.request;


import com.alibaba.fastjson.JSONObject;
import com.momo.common.auth.pojo.AuthUser;
import org.springframework.security.core.context.SecurityContextHolder;

import java.io.Serializable;
import java.util.Map;

public class SecurityContext implements Serializable {

    private AuthUser context;

    public SecurityContext(){
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (principal instanceof Map){
            JSONObject jsonObject = new JSONObject();
            jsonObject.putAll((Map) principal);
            this.context = jsonObject.toJavaObject(AuthUser.class);
        }
    }

    public static AuthUser getContext(){
        return new SecurityContext().context;
    }

}
